package com.fjec.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjec.activity.entity.InfoUserCashingBank;
import com.fjec.activity.mapper.InfoUserCashingBankMapper;
import com.fjec.activity.service.IInfoUserCashingBankService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjec.activity.view.ReqUserCashingBank;
import com.fjec.activity.view.UserCashingBankVO;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.shiro.AccountInfo;
import com.fjec.common.util.DateUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 009.用户代付银行账号信息表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-11-14
 */
@Service
public class InfoUserCashingBankServiceImpl extends ServiceImpl<InfoUserCashingBankMapper, InfoUserCashingBank> implements IInfoUserCashingBankService {

    @Resource
    private InfoUserCashingBankMapper mapper;

    @Override
    public JsonResult registerCashingBank(ReqUserCashingBank reqUserCashingBank) throws Exception {
        if (reqUserCashingBank.getId() == null
                || reqUserCashingBank.getId() == 0L) {
            // 新增
            InfoUserCashingBank entity = new InfoUserCashingBank();
            BeanUtils.copyProperties(entity, reqUserCashingBank);
            entity.setAccountType(0);
            entity.setUserId(AccountInfo.getUserId());
            entity.setCreateTime(DateUtil.nowLdt());
            mapper.insert(entity);
        } else {
            // 更新
            InfoUserCashingBank entity = mapper.selectById(reqUserCashingBank.getId());
            BeanUtils.copyProperties(entity, reqUserCashingBank);
            entity.setUpdateTime(DateUtil.nowLdt());
            mapper.updateById(entity);
        }
        return JsonResult.SUCCESS;
    }

    @Override
    public JsonResult getUserCashingBank() throws Exception {
        Long userId = AccountInfo.getUserId();
        QueryWrapper<InfoUserCashingBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("UserId", userId);
        InfoUserCashingBank entity = mapper.selectOne(queryWrapper);
        UserCashingBankVO view = new UserCashingBankVO();
        if (entity != null) {
            BeanUtils.copyProperties(view, entity);
        }
        return JsonResult.success()
                .data(view)
                .build();
    }

}
